package com.henry.oddeven;

import java.util.Objects;

public class Natural {
    int val;

    public Natural(int valIn) {
        this.val = valIn;
    }

    public Object add(Natural o) throws Exception {
        if (o == null) throw new NullPointerException("Null pointer encountered!");
        int sum = val + o.val;

        if (sum % 2 == 0) return new Even(sum);
        else return new Odd(sum);
    }

    public Object multiply(Natural o) throws Exception {
        if (o == null) throw new NullPointerException("Null pointer encountered!");
        int product = val * o.val;

        if (product % 2 == 0) return new Even(product);
        else return new Odd(product);
    }

    public int getVal() { return val; }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        return this.val == ((Natural)o).val;
    }

    @Override
    public int hashCode() {
        return Objects.hash(val);
    }

    @Override
    public String toString() {
        return "Odd{" + "val=" + val + '}';
    }
}
